﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Threading;
using System.Timers;

namespace test
{
    class Car
    {
        Image carImage;
        private Directon direction;
        private int orientation;
        private Point position;
        private int speed;
        private Canvas canvas;

        //draaien
        int top = 50, left = 100;
        int x = 0;
        int rotatie = 0;
        int draai = 0;
        double sins = 0;
        double coss = 0;
        //

        public Car(Canvas canvasMain, Point point, int speed)
        {
            this.position = point;
            this.canvas = canvasMain;
            this.speed = speed;
            drawCar();
            Thread oThread = new Thread(new ThreadStart(driveCar));
            oThread.Start();
        }

        private void driveCar()
        {

            bool drive = true;
            while (drive)
            {
                changeSpeed(200);
                if (rotatie < 90)
                {
              //      turn(50, "ES", 50, 100);
                }
                carImage.Dispatcher.Invoke(
               new Action(delegate()
               {
                  // position.X = Canvas.GetLeft(carImage);
                   // VisualTreeHelper.HitTest(carImage, new Point(100, 100));

                   position.X = position.X + (speed / 10);
                   Canvas.SetLeft(carImage, position.X);
                   Canvas.SetTop(carImage, position.Y);
                   if (left == 1400) { drive = false; }
               }));

                Thread.Sleep(Properties.Settings.Default.car_frametime);
            }

        }

        private void drawCar()
        {

            carImage = new Image();


            carImage.Source = new BitmapImage(new Uri(@"/test;component/Images/simple-travel-car-top_view.png", UriKind.Relative));

            carImage.Width = 50;
            carImage.RenderTransformOrigin = new Point(0.5, 0.5);
            if (orientation == 1)
            {
                ScaleTransform flipTrans = new ScaleTransform();
                flipTrans.ScaleX = -1;
                //flipTrans.ScaleY = -1;
                carImage.RenderTransform = flipTrans;
            }

            canvas.Children.Add(carImage);
            Canvas.SetLeft(carImage, position.X);
            Canvas.SetTop(carImage, position.Y);
        }



        private void moveForward()
        {

        }

      
        private void turn(int radius, string direction, int topMidCircle, int leftMidCircle)
        {

            x++;
            sins = Math.Sin(x * Math.PI / 180) * radius; //300;
            coss = Math.Cos(x * Math.PI / 180) * radius; //300;
            if (rotatie == 360)
                rotatie = 0;
            if (rotatie == -1)
                rotatie = 359;
            //switch between different direction for the car to turn to.
            switch (direction)
            {
                case "NE":
                    {
                        position.Y = topMidCircle - Convert.ToInt32(sins);
                        position.X = leftMidCircle - Convert.ToInt32(coss);
                        rotatie = rotatie + 1;
                        break;
                    }
                case "EN":
                    {
                        position.X = topMidCircle + Convert.ToInt32(sins);
                        position.Y = leftMidCircle + Convert.ToInt32(coss);
                        rotatie = rotatie - 1;
                        break;
                    }
                case "ES":
                    {
                        position.X = topMidCircle + Convert.ToInt32(sins);
                        position.Y = leftMidCircle - Convert.ToInt32(coss);
                        rotatie = rotatie + 1;
                        break;
                    }
                case "SE":
                    {
                        position.Y = topMidCircle + Convert.ToInt32(sins);
                        position.X = leftMidCircle - Convert.ToInt32(coss);
                        rotatie = rotatie - 1;
                        break;
                    }
                case "SW":
                    {
                        position.Y = topMidCircle - Convert.ToInt32(sins);
                        position.X = leftMidCircle + Convert.ToInt32(coss);
                        rotatie = rotatie + 1;
                        break;
                    }
                case "WN":
                    {
                        position.Y = topMidCircle + Convert.ToInt32(sins);
                        position.X = leftMidCircle + Convert.ToInt32(coss);
                        rotatie = rotatie - 1;
                        break;
                    }
            }
            carImage.Dispatcher.Invoke(
                 new Action(delegate()
                 {

                     carImage.RenderTransform = new RotateTransform(rotatie);
                 }));

            draai++;

        }

        private void changeSpeed(int speedNew)
        {
            if (speedNew > speed)
            {
                speed++; speed++;
            }
            if (speedNew < speed)
            {
                speed--; speed--;
            }
        }
    }
}
